class Solution(object):
    def minAreaRect(self, points):
        points.sort()
        dist = []
        p = []
        n = len(points)
        for i in range(n):
            if not dist:
                dist.append(points[i][0])
                p.append({points[i][1]})
            else:
                if points[i][0] == dist[-1]:
                    p[-1].add(points[i][1])
                else:
                    dist.append(points[i][0])
                    p.append({points[i][1]})
        m = len(dist)
        min_s = 0
        for i in range(m):
            for j in range(i + 1, m):
                t = p[i] & p[j]
                if len(t) >= 2:
                    t = list(t)
                    t.sort()
                    t = [t[i + 1] - t[i] for i in range(len(t) - 1)]
                    if min_s == 0:
                        min_s = (dist[j] - dist[i]) * min(t)
                    else:
                        min_s = min((dist[j] - dist[i]) * min(t), min_s)
        return min_s


data = Solution()
points = [[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]]
print(data.minAreaRect(points))
points = [[1, 1], [1, 3], [3, 1], [3, 3], [4, 1], [4, 3]]
print(data.minAreaRect(points))
